﻿using ProjectCloth.Entity;
using ProjectCloth.FrmUI.ColorCards;
using ProjectCloth.FrmUI.Common;
using ProjectCloth.FrmUI.Companys;
using ProjectCloth.FrmUI.UserControls;
using ProjectCloth.FrmUI.Users;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI
{
    public partial class MainFrm : Form
    {
        private static MainFrm frm = null;
        private ClothContext db = new ClothContext();
        private int count = 0;
        public Color BackgroudColor = Color.LightBlue;

        public void UpdatePermissions(string txt)
        {
            var count = db.Permissions.Where(o => o.Name == txt).Count();
            if (count == 0)
            {
                db.Permissions.Add(new Permission() { Name = txt });
                db.SaveChanges();
            }
        }
        private MainFrm()
        {
            InitializeComponent();
            count++;
            Text = "布行通" + Application.ProductVersion + db.GetFloor();
            loginLab.Text = $"欢迎您，{LoginInfo.GetLoginInfo().UserName}";
            tabPageMenu.BackColor = db.BackgroudColor;// BackgroudColor; 
            AddTab.UpdatePermissioned += UpdatePermissions;
            
        }
        private void ChangeFloor()
        {
            Text = "布行通" + Application.ProductVersion + db.GetFloor();
            loginLab.Text = $"欢迎您，{LoginInfo.GetLoginInfo().UserName}";
            tabPageMenu.BackColor = db.BackgroudColor;// BackgroudColor;
        }

        public static MainFrm CreateInstrance()
        {
            if (frm == null || frm.IsDisposed)
            {
                frm = new MainFrm();
            }
            {
                frm.ChangeFloor();
                return frm;
            }
        }
        private void SaleOrderEample_Click(object sender, EventArgs e)
        {
            new AddTab(new SaleOrderFrm(0), "SaleOrderFrm", "销售单录入(剪货)", tabControl1);
        }

        private void SaleOrderLarge_Click(object sender, EventArgs e)
        {
            new AddTab(new SaleOrderFrm(1), "SaleOrderFrm", "销售单录入(大货)", tabControl1);
        }

        private void TabControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (tabControl1.SelectedTab.Name != "tabPageMenu")
            {
                int index = tabControl1.SelectedIndex;
                tabControl1.TabPages.Remove(tabControl1.SelectedTab);
                tabControl1.SelectedIndex = index - 1;
            }
            else MessageBox.Show("导航页不能关闭！");

        }
        /// <summary>
        /// 关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {

            db.LoginLogs.Add(new LoginLog()
            {
                LogoutTime = DateTime.Now,
                UserId = LoginInfo.GetLoginInfo().UserId.ToString(),
                ClientIp = Dns.GetHostName()
            });
            db.SaveChanges();
            System.Environment.Exit(System.Environment.ExitCode);
            this.Dispose();

        }

        private void SaleListBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new SaleOrderListFrm(), "SaleOrderListFrm", "销售查询", tabControl1);
        }

        private void GeneralExpensesBtn_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("新增一般费用");
            new AddTab(new GeneralExpensesFrm(), "GeneralExpensesFrm", "新增一般费用", tabControl1);
        }

        private void PayableBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new ReceivableAccountListFrm(), "ReceivableAccountListFrm", "收款单查询", tabControl1);
        }

        private void ReceiveBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new ReceivableAccountFrm(), "ReceivableAccountFrm", "收款单", tabControl1);
        }

        private void InOutBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new InOutFlowFrm(), "InOutFlowFrm", "资金流水", tabControl1);
        }

        private void 新增角色ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 新增科目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new SubjectFrm(), "新增科目");
            //new SubjectFrm().Show();
        }

        private void 添加销售商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new ProductFrm(0), "添加销售商品");
            //new ProductFrm(0).Show();
        }

        private void 添加进货商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new ProductFrm(1), "添加进货商品");
            //new ProductFrm(1).Show();
        }

        private void InstockListBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new InStockItemListFrm(), "InStockItemListFrm", "进货查询", tabControl1);
        }

        private void 新增用户ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            new AddTab(new UserFrm(), "新增用户");
            //new UserFrm().Show();
        }

        private void 导入销售商品ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new ImportProduct(), "导入销售商品");
            //new ImportProduct().Show();
        }

        private void 实时资金ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void CaptialBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new CapitalListFrm(), "CapitalListFrm", "实时资金", tabControl1);
        }

        private void 用户角色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new UserRoleFrm(), "UserRoleFrm", "用户权限", tabControl1);
        }

        //private void 添加进货单位ToolStripMenuItem_Click(object sender, EventArgs e)
        //{
        //    new ImportCompanyFrm().ShowDialog();

        //}

        private void GeneralExpensesListBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new GeneralExpListFrm(), "GeneralExpListFrm", "一般费用查询", tabControl1);
        }

        private void QuickInstockBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new SaleToInstockFrm(), "SaleToInstockFrm", "快速进货", tabControl1);
            //new AddTab().AddTabWithControl(new SaleOrderControl(), "SaleOrderControl", "快速进货11", tabControl1);
        }

        private void 导入进货单位ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new ImportCompanyFrm(), "导入进货单位");
            //new ImportCompanyFrm().ShowDialog();
        }

        private void 导入科目ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new ImportSubject(), "导入科目");
            //new ImportSubject().ShowDialog();
        }

        private void SaleStatisticsbtn_Click(object sender, EventArgs e)
        {
            new AddTab(new SalesBook(), "SalesBook", "销售统计", tabControl1);
        }

        private void GeneralExpensesStatisticsbtn_Click(object sender, EventArgs e)
        {
            new AddTab(new GeneralExpensesStatistics(), "GeneralExpensesStatistics", "一般费用统计", tabControl1);
        }

        private void SaleAndInstockBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new SaleTable(), "SaleTable", "销售进货查询", tabControl1);
        }

        private void 销售商品维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new ProductListFrm(), "ProductListFrm", "销售商品维护", tabControl1);
        }

        private void 供应商维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new CompanyListFrm(), "CompanyListFrm", "供应商维护", tabControl1);
        }

        private void 角色权限ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AddTab(new RolePermissionFrm(), "RolePermissionFrm", "角色权限", tabControl1);
        }

        private void 修改登录密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var editpwd = new EditPwdFrm();
            editpwd.ShowDialog();
            if (editpwd.changePwd)
            {
                MessageBox.Show("修改密码成功，将关闭程序！");
                MainFrm_FormClosed(null, null);
                //Application.ExitThread();
                //Application.Exit();
                //Application.Restart();
                //Process.GetCurrentProcess().Kill();
                //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
            }
        }

        private void AddColorCardBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new ColorCardFrms(), "ColorCardFrms", "添加色卡", tabControl1);
        }

        private void ColorCardQuery_Click(object sender, EventArgs e)
        {
            new AddTab(new ColorCardListFrm(), "ColorCardListFrm", "色卡查询", tabControl1);
        }

        private void ColorTJbtn_Click(object sender, EventArgs e)
        {
            new AddTab(new ColorCardTableFrm(), "ColorCardTableFrm", "色卡统计", tabControl1);
        }

        private void 切换楼层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确定要切换楼层吗？", "切换楼层确认框", MessageBoxButtons.OKCancel);
            if (result != DialogResult.OK) return;
            foreach (TabPage p in tabControl1.TabPages)
            {
                if (p.Name != "tabPageMenu")
                {
                    this.tabControl1.TabPages.Remove(p);
                }
            }
            LoginFrm login = new LoginFrm();
            login.ShowDialog();
            Text = "布行通" + Application.ProductVersion + db.GetFloor();
            loginLab.Text = $"欢迎您，{LoginInfo.GetLoginInfo().UserName}";
        }

        private void 软件版本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show($"当前软件版本为{ Application.ProductVersion}");
        }

        private void ColorHytBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new ColorStatsFrm(), "ColorStatsFrm", "色卡频率统计", tabControl1);
        }

        private void 草稿单_Click(object sender, EventArgs e)
        {
            new AddTab(new DraftOrderFrm(), "DraftOrderFrm", "草稿单", tabControl1);
        }

        private void tabPageMenu_Click(object sender, EventArgs e)
        {

        }

        private void AddPriceInfo_Click(object sender, EventArgs e)
        {
            new AddTab(new AskPriceFrm(), "AskPriceFrm", "新增报价信息", tabControl1);
        }

        private void QueryPriceInfoBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new AskPriceListFrm(), "AskPriceListFrm", "查询报价信息", tabControl1);
        }

        private void DraftOrderListBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new DraftOrderListFrm(), "DraftOrderListFrm", "查询草稿单", tabControl1);
        }
        public void SaleOrderLarge(string draftOrderNo)
        {
            new AddTab(new SaleOrderFrm(1, draftOrderNo), "DraftToSaleOrderFrm", "草稿单转录入(大货)", tabControl1);
        }

        private void onsaleProductBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new OnSaleProductFrm(), "OnSaleProductFrm", "商品上架", tabControl1);
        }

        private void onsaleQueryBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new OnSaleProductListFrm(), "OnSaleProductListFrm", "上架频率统计", tabControl1);
        }

        private void simpleStatsBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new SimpleStatsFrm(), "SimpleStatsFrm", "剪版频率统计", tabControl1);
        }

        private void returnSaleOrderBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new SaleOrderListFrm(true), "SaleOrderListFrm", "销售退款单查询", tabControl1);
        }

        private void QuickCheckBtn_Click(object sender, EventArgs e)
        {
            new AddTab(new QuickCheckSaleFrm(), "QuickCheckSaleFrm", "快速审核", tabControl1);
        }
    }
}
